package gold.digger;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC400 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {

        public int limit = 10;

        public int findNthDigit(int n) {
            if (n <= 9) return n;

            int[] digits = new int[limit];
            digits[0] = 9;
            int i = 1;
            for (; i < limit; i++) {
                digits[i] = (int) ((i + 1) * 9 * Math.pow(10, i) + digits[i - 1]);
                if (n > digits[i - 1] && n <= digits[i]) break; //n is i+1 digit number
            }

            n -= digits[i - 1];
            int num = (int) (Math.pow(10, i) + (n - 1) / (i + 1));
            int k = (n - 1) % (i + 1);
            return String.valueOf(num).charAt(k) - '0';
        }
    }

    public void run() {
        Solution solution = new Solution();
        System.out.println(solution.findNthDigit(12));
    }

    public static void main(String[] args) throws Exception {
        LC400 an = new LC400();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
